package com.zygk.locationtool.dao;

import android.content.Context;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.rest.Response;
import com.yanzhenjie.nohttp.rest.SimpleResponseListener;
import com.yanzhenjie.nohttp.rest.StringRequest;
import com.zygk.library.util.CallServer;
import com.zygk.library.util.JsonUtil;
import com.zygk.library.util.ToastUtil;
import com.zygk.locationtool.model.apimodel.APIM_ProjectList;
import com.zygk.locationtool.util.HttpRequest;

import static com.zygk.locationtool.config.Urls.PROJECT;

/**
 * Created by Wikison on 2018/12/14.
 */
public class CommonLogic {

  /**
   * 项目列表
   */
  public static void getBaseProjectsList(final Context mContext,
      final HttpRequest.HttpCallback callback) {
    StringRequest request = new StringRequest(PROJECT, RequestMethod.GET);

    SimpleResponseListener<String> listener = new SimpleResponseListener<String>() {
      @Override public void onStart(int what) {
        super.onStart(what);
        callback.onStart();
      }

      @Override public void onSucceed(int what, Response<String> response) {
        super.onSucceed(what, response);
        APIM_ProjectList result = JsonUtil.jsonToObject(response.get(), APIM_ProjectList.class);

        if (result == null) return;

        if (result.getStatus() == 1) {
          callback.onSucceed(result);
        } else {
          ToastUtil.showMessage(mContext, result.getMessage());
        }
      }

      @Override public void onFailed(int what, Response<String> response) {
        super.onFailed(what, response);
        callback.onFailed();
      }

      @Override public void onFinish(int what) {
        super.onFinish(what);
        callback.onFinish();
      }
    };
    CallServer.getInstance().request(0, request, listener);
  }
}
